<template>
    <div class="home">
        <el-container class="wrapper">
            <!-- 头部区域 -->
            <el-header>
                <div class="header">
                    <img src="../assets/logo.png" alt="" />
                    <span>电商后台管理系统</span>
                </div>
                <el-button type="info" @click="exit">退出</el-button>
            </el-header>
            <el-container>
                <!-- 左边导航目录部分 -->
                <el-aside :width="isCollapse ? '61px' : '200px'">
                    <div class="toggle-button" @click="toggleClick">|||</div>
                    <!-- 左侧菜单 -->
                    <el-menu :collapse="isCollapse" :unique-opened="true"
                    :collapse-transition="false" router
                    background-color="#333744" text-color="#fff" active-text-color="#409EFF"
                    :default-active="activePath"
                    :default-openeds="defaulopened">
                        <el-submenu :index="index+''" v-for="(item,index) in menuList" :key="index">
                            <template slot="title">
                                <i :class="icon[index]"></i>
                                <span>{{ item.authName }}</span>
                            </template>
                            <el-menu-item v-for="(item2,index2) in item.children" :key="index2" 
                            :index="item2.path"
                            @click="saveActivePath(item2.path)">
                                <template slot="title">
                                    <i class="el-icon-menu"></i>
                                    <span>{{ item2.authName }}</span>
                                </template>
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <!-- 右边主体部分 -->
                <el-main>
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
import { request } from '../api/axios'
export default {
    name: 'Home',
    data() {
        return {
            // 获取到的菜单数据
            menuList: [],
            icon: [
                "el-icon-user-solid","el-icon-s-management","el-icon-s-goods","el-icon-s-order","el-icon-s-marketing"
            ],
            isCollapse: false, // 是否折叠属性
            activePath: '',
            defaulopened:["0"]
        }
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
        // 获取左侧菜单
        async getMenuList() {
            await request("get","/menus").then(res=>{   //1.4.2
                // console.log(res)
                if(res.meta.status==200){
                    this.menuList=res.data
                }
                else{
                    this.$message.error(res.meta.msg);
                }
            })
        },

        // 点击按钮,菜单的折叠和展开
        toggleClick(){
            this.isCollapse=!this.isCollapse
            console.log(this.isCollapse)
        },

        // 保存菜单栏链接的激活状态
        saveActivePath(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath = activePath
            console.log(this.$route.path)
        },

        // 点击退出登录
        exit(){
            this.$confirm('您确定要退出登录吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                window.sessionStorage.clear()
                window.localStorage.clear();
                this.$router.push('/login')
            })
        },
    },
}
</script>

<style scoped>
.wrapper{
    /* position: fixed; */
    position: absolute;
    width: 100%;
    height: 100%;
}
.el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    background-color: #373d3f;
    color: #fff;
    font-size: 20px;
}
.header{
    display: flex;
    align-items: center;
}
.header img{
    width: 60px;
    height: 60px;
}
.header span{
    margin-left: 15px;
}

/* 左侧菜单栏 */
.el-aside {
    background-color: #333744;
}
.el-menu {
    border-right: none;
}
.el-main {
    width: 100%;
    background-color: #eaedf1;
}

/* .iconfont {
  padding-right: 10px;
} */
.toggle-button {
    background: #4a5064;
    font-size: 10px;
    text-align: center;
    line-height: 24px;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>